#!/bin/sh -f
# $Id: repo.sh 18da6ae1f8af 2012/12/02 10:02:40 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# __version__  = "$Revision: 18da6ae1f8af $"
# __author__   = "$Author: pgurumur $"
# __modified__ = "$Date: 2012-12-02 10:02:40Z $"

PROG=`basename $0`
UID=`id -u`
CHROOT=`which chroot`
FCGI=`which spawn-fcgi`
CONFIG=

usage () {
   echo "${PROG} [ options ] [ start|stop|restart|status ]"
   echo "options: "
   echo "\t-c [ configuration file, ususally chroot.cfg ]"
   echo "\t-h [ prints this help ]"
   exit 1

}

check_config () {
   [ -z ${CONFIG} ] && error "cannot proceed without config file"
   [ -f ${CONFIG} ] || error "configuration file non existent"
}

error () {
   mesg=$*
   write ${mesg}
   exit 1
}

write () {
   mesg=$*
   echo "${PROG}: ${mesg}"
}

repo_status () {
   check_config
   PID=0
   SSHPIDFILE=`sed -n 's/sshpidfile = \(.*\)/\1/p' ${CONFIG}`
   [ -f ${SSHPIDFILE} ] && PID=`awk '{ print $0; }' ${SSHPIDFILE}`
   [ ${PID} -ne 0 ] && write "chroot sshd running, pid: ${PID}"
   
   PID=0
   HGFCGIPIDFILE=`sed -n 's/hgfcgipidfile = \(.*\)/\1/p' ${CONFIG}`
   [ -f ${HGFCGIPIDFILE} ] && PID=`awk '{ print $0; }' ${HGFCGIPIDFILE}`
   [ ${PID} -ne 0 ] && write "hgweb fcgi running, pid: ${PID}"

   NGINXFILE=`sed -n 's/nginxfile = \(.*\)/\1/p' ${CONFIG}`
   [ -f ${NGINXFILE} ] && write "nginx running"
}

repo_start () {
   check_config
   PID=0
   echo -n "starting dameons: "
   CHROOT_DIR=`sed -n 's/chroot = \(.*\)/\1/p' ${CONFIG}`
   USER=`sed -n 's/user = \(.*\)/\1/p' ${CONFIG}`
   GROUP=`sed -n 's/group = \(.*\)/\1/p' ${CONFIG}`
   HGSOCK=`sed -n 's/hgsocket = \(.*\)/\1/p' ${CONFIG}`
   HGFILE=`sed -n 's/hgwebfile = \(.*\)/\1/p' ${CONFIG}`
   SSHPIDFILE=`sed -n 's/sshpidfile = \(.*\)/\1/p' ${CONFIG}`
   [ -f ${SSHPIDFILE} ] && PID=`awk '{ print $0; }' ${SSHPIDFILE}`
   if [ ${PID} -eq 0 ]; then
      ${CHROOT} ${CHROOT_DIR} /usr/sbin/sshd
      echo -n 'sshd '
   fi

   PID=0
   HGFCGIPIDFILE=`sed -n 's/hgfcgipidfile = \(.*\)/\1/p' ${CONFIG}`
   [ -f ${HGFCGIPIDFILE} ] && PID=`awk '{ print $0; }' ${HGFCGIPIDFILE}`
   if [ ${PID} -eq 0 ]; then
      TEMP="/var/www${HGSOCK}"
      [ -S ${TEMP} ] && rm -f ${TEMP}
      TEMP="/var/www/logs/fastcgi/hgweb.pid"
      [ -f ${TEMP} ] && rm -f ${TEMP}
      ${FCGI} -c ${CHROOT_DIR} -u ${USER} -g ${USER} -s ${HGSOCK} ${HGFILE} > /dev/null 2>&1
      echo -n 'hgfcgi '
   fi

   NGINXFILE=`sed -n 's/nginxfile = \(.*\)/\1/p' ${CONFIG}`
   if [ ! -f ${NGINXFILE} ]; then
      /usr/sbin/nginx -t > /dev/null 2>& 1
      [ $? -eq 0 ] && /etc/rc.d/nginx start > /dev/null 2>&1 && echo -n 'nginx '
   fi

   echo "."
}

repo_stop () {
   check_config
   PID=0
   echo -n "stopping dameons: "
   CHROOT_DIR=`sed -n 's/chroot = \(.*\)/\1/p' ${CONFIG}`
   USER=`sed -n 's/user = \(.*\)/\1/p' ${CONFIG}`
   GROUP=`sed -n 's/group = \(.*\)/\1/p' ${CONFIG}`
   HGSOCK=`sed -n 's/hgsocket = \(.*\)/\1/p' ${CONFIG}`
   HGFILE=`sed -n 's/hgwebfile = \(.*\)/\1/p' ${CONFIG}`
   SSHPIDFILE=`sed -n 's/sshpidfile = \(.*\)/\1/p' ${CONFIG}`
   [ -f ${SSHPIDFILE} ] && PID=`awk '{ print $0; }' ${SSHPIDFILE}`
   if [ ${PID} -ne 0 ]; then
      kill -TERM ${PID}
      echo -n 'sshd '
   fi

   PID=0
   HGFCGIPIDFILE=`sed -n 's/hgfcgipidfile = \(.*\)/\1/p' ${CONFIG}`
   [ -f ${HGFCGIPIDFILE} ] && PID=`awk '{ print $0; }' ${HGFCGIPIDFILE}`
   if [ ${PID} -ne 0 ]; then
      TEMP="/var/www${HGSOCK}"
      [ -S ${TEMP} ] && rm -f ${TEMP}
      TEMP="/var/www/logs/fastcgi/hgweb.pid"
      [ -f ${TEMP} ] && rm -f ${TEMP}
      kill -TERM ${PID}
      echo -n 'hgfcgi '
   fi

   NGINXFILE=`sed -n 's/nginxfile = \(.*\)/\1/p' ${CONFIG}`
   if [ -f ${NGINXFILE} ]; then
      /etc/rc.d/nginx stop > /dev/null 2>& 1 && echo -n 'nginx '
   fi

   echo "."
}

repo_restart () {
   repo_stop
   repo_start
}

if [ $# -lt 1 ]; then
   usage
else
   while getopts c:h arg; do
      case ${arg} in
         c)
            CONFIG=${OPTARG}
            ;;
         
         h)
            usage
            ;;

         *)
            usage
            ;;
      esac
   done

   if [ ${UID} -ne 0 ]; then
      echo "${PROG}: need root privileges"
      exit 1
   fi

   arg=`echo $* | awk '{ print $NF; }'`
   case ${arg} in
      start)
         repo_start
         ;;

      stop)
         repo_stop
         ;;

      restart)
         repo_start
         ;;

      status)
         repo_status
         ;;

      *)
         usage
         ;;
   esac
fi
